<?php
namespace Home\Controller;
use Think\Controller;
use Common\Controller\CommonController;
use Org\Util\Step;
use Think\page;
class BowenController extends CommonController {
	//展示博文列表的页面
	public function index(){
		$where = array('del' => 0);
		import('Think.Page');
		$count = D('bokegl')->relation(true)->where($where)->count();	
		$page  = new page($count,10);
		$show = $page->show(); // 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$boke = D('bokegl')->relation(true)->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('boke',$boke);
		$this->display();		
	}
	//展示添加博文的页面
	public function addbowen(){
		//文章分类
		import("Org/Util/Step");
		$m = M('step');
		$step = $m->order('sort')->select();
		$step = Step::fenlei($step);
		//文章属性
		$m1 = M('attr');
		$attr = $m1->select();
		$this->assign('attr',$attr);
		$this->assign('step',$step);
		$this->display();		
	}
	//处理添加博文的操作
	public function doaddbowen(){
		if(!($_POST['title']&&$_POST['content']&&$_POST['abstract']&&$_POST['cid'])){
			$this->error('请填写完成信息');
		}
		$data = array(
			'title' => $_POST['title'],
			'content' => $_POST['content'],	
			'abstract' =>$_POST['abstract'],
			'time' => time(),
			'cid' => $_POST['cid']
		);		
		//判断是否添加了属性
		if(isset($_POST['aid'])){			
			foreach($_POST['aid'] as $v){
				$data['attr'][] = $v;
			}
		}
		$d = D('Bokegl')->relation(true)->add($data);
		if($d>0){
			$this->success('博文发布成功',U('Bowen/index'));
		}else{
			$this->error('博文发布失败');			
		}
	}
	//展示回收站的数据
	public function recycle(){
		$where = array('del' => 1);
		$boke = D('bokegl')->relation(true)->where($where)->select();
		$this->assign('boke',$boke);
		$this->display();		
	}
	//处理删除到回收站的操作
	public function torecycle(){
		$m = M('boke');
		$id = $_GET['id'];
		$update = array(
			'id' => $id,
			'del' => 1	
		);
		if($m->save($update)){
			$this->success('删除成功',U('Bowen/index'));
		}else{
			$this->success('删除失败',U('Bowen/index'));
		}		
	}
	//处理回收站中的操作
	public function restore(){
		$m = M('boke');
		$id = $_GET['id'];
		$update = array(
				'id' => $id,
				'del' => 0
		);
		if($m->save($update)){
			$this->success('还原成功',U('Bowen/index'));
		}else{
			$this->success('还原失败',U('Bowen/index'));
		}		
	}
	//处理彻底删除操作
	public function realdel(){
		$id = $_GET['id'];
		$where = array(
			'id' => $id
		);
		$m = D('bokegl')->relation('attr')->where($where)->delete();		
		if($m){
			M('words')->where(array('pid'=>$id))->delete();
			$this->success('删除成功',U('Bowen/index'));
		}else{
			$this->success('删除失败',U('Bowen/index'));
		}
	}
	//全部删除
	public function delall(){
		$m = M('boke');
		$sum = $m->where(array('del'=>1))->select();		
		foreach($sum as $k){
			D('bokegl')->relation('attr')->where(array('id'=>$k['id']))->delete();
			M('words')->where(array('pid'=>$k['id']))->delete();
		}		
		$this->redirect('Bowen/recycle');		
	}
	//展示修改页面
	public function xiugai(){
		$id = $_GET['id'];
		$where = array(
			'id' => $id				
		);
		//文章分类
		import("Org/Util/Step");
		$m = M('step');
		$step = $m->order('sort')->select();
		$step = Step::fenlei($step);
		$this->assign('step',$step);
		//文章属性
		$m1 = M('attr');
		$attr = $m1->select();
		$this->assign('attr',$attr);
		//文章内容
		$boke = D('bokegl')->relation(true)->where($where)->find();			
		$this->assign('boke',$boke);
		$this->display();		
	}	
	//执行修改操作
	public function doxiugai(){			
		$where = array(
			'id' => $_POST['id']
		);
		$update = array(				
				'title' => $_POST['title'],
				'content' => $_POST['content'],
				'abstract' =>$_POST['abstract'],
				'time' => time(),
				'cid' => $_POST['cid']
		);
		if(isset($_POST['aid'])){
			foreach($_POST['aid'] as $v){
				$update['attr'][] = $v;
			}
		}else{
			$where1 = array(
					'bid' => $_POST['id']
			);
			M('boke_attr')->where($where1)->delete();
		}
		$d = D('bokegl')->relation('attr')->where($where)->save($update);
		if($d){
			$this->success('修改成功',U('Bowen/index'));
		}else{
			$this->success('修改失败',U('Bowen/index'));
		}		
	}
	//查看留言
	public function readwords(){
		$pid = $_GET['pid'];
		$words = M('words')->where(array('pid'=>$pid))->select();
		$this->assign('words',$words);
		$this->display();
	}
	//删除留言
	public function delwords(){
		$id = $_GET['id'];
		$sum = M('words')->where(array('id'=>$id))->find();			
		$count = M('words')->where(array('id'=>$id))->delete();
		if($count){
			$this->success('删除成功',U('Bowen/readwords',array('pid'=>$sum['pid'])));
		}else{
			$this->error('删除失败',U('Bowen/readwords',array('id'=>$id)));
		}		
	}	
}
?>